package com.znyj.servlet.sqlHelper;


/**
 * Created by nameless on 15-2-15.
 */
public class SqlProvider {

    public String query(SqlRequest request) {
        String sql = request.makeSql();
        return sql;
    }

    public String count(SqlRequest request) {
        String oldTargetField = request.getTargetFields();
        request.setTargetFields(" count(1) ");
        String sql = request.makeSql();
        request.setTargetFields(oldTargetField);
        return sql;
    }

    public String countByExp(String exp) {
        String sql = "select count(*) from cdb_factory where cast(device_id as CHAR) REGEXP '" + exp + "'";
        return sql;
    }

    public String findLast(String deviceId) {
        String sql = "select * from (select * from cdb_task where device_id='" + deviceId + "' order by id desc limit 10 )a where control_msg like 'cdb%' limit 1";
        return sql;
    }

    public String findBindMerchant(String deviceId) {
        String sql = "select * from cdb_merchant where device_list like '%" + deviceId + "%'";
        return sql;
    }
    //    public String targetCount(SqlRequest request) {
//        String oldTargetField = request.getTargetFields();
//        request.setTargetFields(" count(" + oldTargetField + ") ");
//        String sql = request.makeSql();
//        request.setTargetFields(oldTargetField);
//        return sql;
//    }
//
//    public String distinctCount(SqlRequest request) {
//        String oldTargetField = request.getTargetFields();
//        request.setTargetFields(" count(distinct " + oldTargetField + ") ");
//        String sql = request.makeSql();
//        request.setTargetFields(oldTargetField);
//        return sql;
//    }
//
//    public String batchInsertStockDeviceLog(List<StockDeviceLog> list) {
//        StringBuilder stringBuilder = new StringBuilder();
//        stringBuilder.append(
//            "insert into  stock_device_log (id, sn_code, operation_type,  operator,"
//            + " operation_id, utime, ctime, action) values ");
//        MessageFormat messageFormat = new MessageFormat(
//            "(#'{'list[{0}].id},#'{'list[{0}].sn_code},#'{'list[{0}].operation_type},#'{'list[{0}].operator},"
//            + "#'{'list[{0}].operation_id},#'{'list[{0}].utime},#'{'list[{0}].ctime},#'{'list[{0}].action})");
//        for (int i = 0; i < list.size(); i++) {
//            stringBuilder.append(messageFormat.format(new Integer[]{i}));
//            stringBuilder.append(",");
//        }
//        stringBuilder.setLength(stringBuilder.length() - 1);
//        return stringBuilder.toString();
//    }
//
//    public String batchInsertStockDevice(List<StockDeviceLog> list) {
//        StringBuilder stringBuilder = new StringBuilder();
//        stringBuilder.append("insert into  stock_device_log (id, device_id, category, sn_code, imel, "
//                             + "hardware_communication_board_ver, hardware_control_board_ver, "
//                             + "firmware_ver, protocol_ver, device_status, ctime, utime, action) values ");
//        MessageFormat messageFormat = new MessageFormat(
//            "(#'{'list[{0}].id},#'{'list[{0}].device_id},#'{'list[{0}].category},#'{'list[{0}].sn_code},"
//            + "#'{'list[{0}].imel},#'{'list[{0}].hardware_communication_board_ver},#'{'list[{0}].hardware_control_board_ver},"
//            + "#'{'list[{0}].firmware_ver},#'{'list[{0}].protocol_ver},#'{'list[{0}].device_status},"
//            + "#'{'list[{0}].ctime},#'{'list[{0}].utime},#'{'list[{0}].action})");
//        for (int i = 0; i < list.size(); i++) {
//            stringBuilder.append(messageFormat.format(new Integer[]{i}));
//            stringBuilder.append(",");
//        }
//        stringBuilder.setLength(stringBuilder.length() - 1);
//        return stringBuilder.toString();
//    }
}
